<template>
	<view>
		<view class="box">
			<image lazy-load="true" :src="img != ''?$store.state.user.icon:img" class="img"></image>
		</view>
		
		
		<view style="position: fixed;bottom: 0;">
				<button style="margin: 0px;width: 100vw;color: #000000;" class="cu-btn bg-white margin-tb-sm lg"  @click="submit()">选择图片</button>
		</view>
	</view>
</template>

<script>
	var _self
	export default {
		components: {
			
		},
		onLoad() {
			_self = this
			_self.img = _self.$store.state.user.icon 
			switch(_self.$store.state.platform){
				case 'android':
				   console.log('运行Android上') 
				   Vue.prototype.requestAndroidPermission("android.permission.CAMERA")
				   break;
				case 'ios':
				   Vue.prototype.requestIosPermission("camera","摄像头")
				   Vue.prototype.requestIosPermission("photoLibrary","相册")
				   console.log('运行iOS上')
				   break;
				default:
				   console.log('运行在开发者工具上')
				   break;
			}
		},
		onShow() {
			
		},
		onUnload(){

			
		},
		watch:{
			
		},
		data() {
			return {
				img: ''
			}
		},
		methods: {
			submit(){
				if(_self.img != ""){
					uni.chooseImage({
					     count: 1, //默认9
					     success: function (res) {
							 _self.img = res.tempFilePaths[0]
							 uni.showLoading({
							     title: '上传中'
							 });
							 _self.xhttp.upload('/user/upload/icon',{
							 	filePath: _self.img,
							 	name: 'icon', 
							 	header:{
							 		'content-type': 'multipart/form-data'  
							 	},
							 }).then(res => {
							 	console.log(res)
							 	if(res.data.code == 200){
							 		uni.showLoading({
							 		    title: '修改中'
							 		});
									var imgUrl = res.data.data.imgUrl
							 		_self.xhttp.request({
							 			url: "/user/updateInfo", //仅为示例，并非真实接口地址。
							 			method:"POST",
							 			data: {
							 				name:_self.$store.state.user.name,
							 				icon: imgUrl 
							 			},
							 			header:{
							 				'Content-Type':'application/json'
							 			}
							 		}).then(res => {
							 			if(res.data.code == 200){					
							 				uni.navigateBack({
							 					delta:1
							 				})
							 			  _self.toast("更换成功")
							 			}
							 		})
							 	} 
							 })
					     }
					 });
				}else{
					_self.toast("请选择图片")
				}
			}
		}
	}
</script>

<style scoped>
	.button-box {
		margin: 30upx;
		text-align: center;
		
	}

	.button-box1 {
		
		width: 50%;
	}

	.box {
		margin: 2% 30%;
		box-shadow: 0 0 5upx ;
		width: 40%;
		height: 40vw;

	}

	.img {
		width: 100%;
		height: 100%;
	}
	
	button{
		border-radius: 0;
	}

	button::after {
		border: none;
	}

</style>
